package org.cnam.mobileShopping.dao;

import java.util.Date;
import java.util.List;

import org.cnam.mobileShopping.bean.Userlist;
import org.cnam.mobileShopping.sql.DbAdapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Classe de DAO pour les UserList
 */
public class UserListDAO extends DAO<Userlist> {

	private static final String tableName = "userlist";

	/**
	 * Constructeur par défaut lançant la création de toutes les connexions
	 */
	public UserListDAO(Context context) {
		sqlHelper = new DbAdapter(context);
	}

	// usermlistid INTEGER PRIMARY KEY AUTOINCREMENT,userid int,mlistid int
	// references mList,status smallint

	@Override
	public long create(Userlist obj) {
		// récupération d'un produit dans la base de données locale
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("userid", obj.getUserid());
		cv.put("mlistid", obj.getMlistid());
		cv.put("status", obj.getStatus());
		long toReturn = db.insert(tableName, "", cv);
		db.close();
		return toReturn;
	}

	public boolean delete(Integer userListId) {
		SQLiteDatabase db = sqlHelper.getReadableDatabase();
		db.delete(tableName, "usermlistid=?",
				new String[] { userListId.toString() });
		db.close();
		return true;
	}

	@Override
	public boolean update(Userlist obj) {
		// envoit du produit à la base de données distante
		// TODO Send to the web service
		// récupération d'un produit dans la base de données locale
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("usermlistid", obj.getUsermlistid());
		cv.put("userid", obj.getUserid());
		cv.put("mlistid", obj.getMlistid());
		cv.put("status", obj.getStatus());
		db.update(tableName, cv, "usermlistid=?", new String[] { obj
				.getUsermlistid().toString() });
		db.close();
		return true;
	}

	@Override
	public boolean delete(Userlist obj) {
		return delete(obj.getUsermlistid());
	}

	@Override
	public Userlist find(int id) {
		Userlist toReturn = null;
		SQLiteDatabase db = sqlHelper.getReadableDatabase();
		Cursor cursor = db.query(tableName, new String[] { "usermlistid",
				"userid", "mlistid", "status" }, "usermlistid=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				toReturn = new Userlist();
				toReturn.setUsermlistid(cursor.getInt(0));
				toReturn.setUserid(cursor.getInt(1));
				toReturn.setMlistid(cursor.getInt(2));
				toReturn.setStatus(cursor.getShort(3));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		db.close();
		return toReturn;
	}

	@Override
	public List<Userlist> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Userlist findByCommonId(String commonid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void synchronise(Date lastupdate, Integer userId, Context context) {
		// TODO Auto-generated method stub

	}

}
